package com.kly.user.utils;


import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

public class UserUtil {
    public static void getUserCode(){
    }

    public static int generateWorkerId() {
        InetAddress address;
        try {
            // 获取本地IP地址
            address = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            throw new RuntimeException("Unable to get local host address.", e);
        }

        // 提取IP地址的最后一段作为workerId
        byte[] ipAddressByteArray = address.getAddress();
        int workerId = (ipAddressByteArray[ipAddressByteArray.length - 1] & 0xFF) % 256;


        return workerId;
    }

    public static  <T> Map<String, T> listToMap(List<T> list, Function<T, String> keyExtractor) {
        Map<String, T> map = new HashMap<>();
        for (T item : list) {
            map.put(keyExtractor.apply(item), item);
        }
        return map;
    }

    public static void main(String[] args) {
        System.out.println(generateWorkerId());
    }
}
